package com.yimi.yinhepda.views.moveStock_module;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.RecycleAdapter.MoveStockCacheAdapter;
import com.pda.ym.base.CommonViewHolder;
import com.pda.ym.base.BaseTitleActivity;
import com.yimi.yinhepda.db.dao.MoveStockTaskCacheTableDao;
import com.yimi.yinhepda.db.dao.MoveStockScanMasterTableDao;
import com.yimi.yinhepda.db.dao.MoveStockScanTableDao;
import com.yimi.yinhepda.db.table.MoveStockTaskCacheTable;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.SoundVibratorManager;
import com.yimi.yinhepda.views.load_module.LoadUnloadGroupActivity;
import com.yimi.yinhepda.views.moveStock_module.load.MoveStockLoadScanActivity;
import com.yimi.yinhepda.views.moveStock_module.unload.MoveStockUnloadScanActivity;

import org.jetbrains.annotations.NotNull;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: lj
 * Date: 2018/12/26
 * Description: 移库装车任务暂存
 */
public class MoveStockCacheActivity extends BaseTitleActivity implements CommonViewHolder.onItemCommonClickListener {

    private RecyclerView rvCacheList;
    private MoveStockCacheAdapter cacheAdapter;
    private List<MoveStockTaskCacheTable> entities;
    private MoveStockTaskCacheTable cacheEntity;
    public static Handler mhander;

    @Override
    public String getPageName() {
        return "移库任务暂存集";
    }

    @Override
    public int setContentViewId() {
        return R.layout.activity_move_stock_cache;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        rvCacheList = findViewById(R.id.rv_cache_list);
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.iv_delete).setOnClickListener(this);
        findViewById(R.id.iv_exec).setOnClickListener(this);


        entities = new ArrayList<>(0);
        cacheAdapter = new MoveStockCacheAdapter(this, entities, this);
        rvCacheList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rvCacheList.setAdapter(cacheAdapter);

        loadData();
    }

    @SuppressLint("HandlerLeak")
    @Override
    protected void onStart() {
        super.onStart();
        mhander = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                loadData();
            }
        };
    }

    private void loadData() {
        List<MoveStockTaskCacheTable> taskCacheTableList = new MoveStockTaskCacheTableDao().findByUpload(0, 1);
        if (taskCacheTableList != null) {
            entities.clear();
            entities.addAll(taskCacheTableList);
            cacheAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_delete:
                //删除按钮
                if (cacheEntity == null || !cacheEntity.isCheck()) {
                    showToast("请选择一项删除！");
                    SoundVibratorManager.playSound(2);
                    return;
                }
                try {
                    new MoveStockTaskCacheTableDao().delete(cacheEntity);
                    new MoveStockScanMasterTableDao().deleteByTaskNo(cacheEntity.getTaskNo(), cacheEntity.getOperType());
                    new MoveStockScanTableDao().deleteByTaskNo(cacheEntity.getTaskNo(), cacheEntity.getOperType());
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                loadData();
                SoundVibratorManager.playSound(1);
                break;
            case R.id.iv_exec:
                if (cacheEntity != null && cacheEntity.isCheck()) {
                    Intent intent = null;
                    //装车类型：1-移库装车，2-移库卸车
                    if (cacheEntity.getOperType() == 1) {
                        //移库装车
                        if (!sharedPreferUtil.getBooleanValue(Common.HAS_UNLOAD_GROUP)) {
                            intent = new Intent(this, LoadUnloadGroupActivity.class);
                        } else {
                            intent = new Intent(this, MoveStockLoadScanActivity.class);
                        }
                        intent.putExtra("scanType", 5);
                    } else {
                        //移库卸车
                        if (!sharedPreferUtil.getBooleanValue(Common.HAS_UNLOAD_GROUP)) {
                            intent = new Intent(this, LoadUnloadGroupActivity.class);
                        } else {
                            intent = new Intent(this, MoveStockUnloadScanActivity.class);
                        }
                        intent.putExtra("scanType", 6);
                    }
                    intent.putExtra("CacheTableEntity", cacheEntity);
                    startActivity(intent);
                } else {
                    showToast("请选择一项任务执行！");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClickListener(int position) {
        cacheEntity = entities.get(position);
        if (cacheEntity.isCheck()) {
            cacheEntity.setCheck(false);
        } else {
            for (MoveStockTaskCacheTable item : entities) {
                if (item.equals(cacheEntity)) {
                    item.setCheck(true);
                } else {
                    item.setCheck(false);
                }
            }
        }
        cacheAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemLongClickListener(int position) {

    }

    @Override
    public boolean showBack() {
        return false;
    }

    @Override
    public void setBack() {

    }

    @NotNull
    @Override
    public String setTitle() {
        return getPageName();
    }

}
